const config = require('../config');
const { API_BASE_URL } = config;
const swiperList = [
  '/image/轮播图1.jpg',
  '/image/轮播图2.jpg',
  '/image/轮播图3.jpg',
  '/image/轮播图4.jpg'
];
Page({
  data: {
    current: 1,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList,
    goodsList: [],
    img1: '/image/苹果.png',
    img2: '/image/优惠券.png',
    img3: '/image/铲子.png',
    img4:'/image/火鸡.png'
  },
  onLoad() {
    //获取轮播图详情
    this.getBanner()
    //获取分类
    this.getTypesList()
    //获取商品列表
    this.getGoodsList()
  },
  //轮播图
  getBanner(){
  },
  //首页分类
  getTypesList(){

  }
  ,
  //商品列表
  getGoodsList(){

    const that = this; // 保存this的引用，因为wx.request的回调函数中this指向会变化
    const token = wx.getStorageSync('token');
    console.log("存储 token:", token);

    if (!token) {
      console.warn('没有找到 token');
      return;
    }

    wx.request({
      url: `${API_BASE_URL}/product/list/page/vo`, // 使用实际接口URL
      method: 'POST',
      data: {
        current: that.data.current,
        size: that.data.size
      },
      header: {
        'content-type': 'application/json', // 默认值是 application/json
        'satoken': token // 添加 token 到请求头
      },
      success(res) {
        const records = res.data.data.records || [];
        console.log("goodList",records)
        that.setData({
          goodsList: records,
          total: res.data.data.total, // 假设响应中有总记录数
          pages: res.data.data.pages  // 假设响应中有总页数
        });
      },
      fail(err) {
        console.error('网络请求失败', err);
      }
    });
  }
  ,
  //轮播图跳转
  toBannerDetail(event){
    console.log(event)
    wx.navigateTo({
      url: '/pages/first/bannerDetail',
    })
  },
  //商品详情跳转
  toGoodDetail(event){
    console.log(event)
    let id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail?id='+id,
    })
  },
  //分类详情跳转
  toTypeDetail(event){
    console.log(event.currentTarget.dataset.id)
    let id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/first/typedetail/typedetail?id='+id,
    })
  },
  //首页搜索栏跳转
  toSearch: function() {  
    wx.navigateTo({  
      url: '/pages/first/search/search',
    });  
  }
})